package cn.snow.boot.datasource;

/**
 * 动态切换的 Holder 类
 *
 * @author liuwanxiang
 * @version 2019/05/28
 */
public class DatasourceContextHolder {

    private static final ThreadLocal<DbName> DATABASE_NAME = new ThreadLocal<>();

    public static DbName getDatabase(){
        return DATABASE_NAME.get()==null? DbName.PRIMARY:DATABASE_NAME.get();
    }

    public static void setDatabase(DbName dbName){
        if (dbName == null){
            throw new NullPointerException("Database name not allow be null~");
        }
        DATABASE_NAME.set(dbName);
    }

    public static void clearDatabase(){
        DATABASE_NAME.remove();
    }

}
